//
//  GYUserFootViewController.m
//  GoodLuck
//
//  Created by 郭为勇 on 2018/3/5.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

static NSString *footCellIdentifier = @"footCellIdentifier";

#import "GYUserFootViewController.h"
#import "GYNoAddressView.h"
#import "GYFootCell.h"
#import "GYNetWorkTools.h"
#import "GYShopHomeViewController.h"

@interface GYUserFootViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDataSource>

/** 没有足迹的视图 */
@property (nonatomic, strong) GYNoAddressView *noFootView;

/** 视图 */
@property (nonatomic, strong) UICollectionView *collectionView;

/** 数据源 */
@property (nonatomic, strong) NSMutableArray *footList;

@property (nonatomic, assign) NSInteger currentIndex;

@property (nonatomic, getter=isHasmore) BOOL hasmore;

@end

@implementation GYUserFootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.currentIndex = 1;
    self.hasmore = YES;
    //初始化
    [self setupUI];
    
    //加载数据
    [self loadFootData:@"1"];
    
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    self.collectionView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight-(iPhoneX ? 44 : 0));
    self.noFootView.frame = self.view.bounds;
}

- (void)setupUI {
    self.navigationItem.title = @"浏览记录";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"清空" style:UIBarButtonItemStylePlain target:self action:@selector(removeAllUserFootprint)];
    
    //初始化视图
    [self setupCollectionView];
    
    //加载没有商品的视图
    self.noFootView = [self getNoShopView:@"goods-browse_w" head:@"暂无您的浏览记录" foot:@"可以去看看那些想要买的"];
    __weak typeof(self) weakSelf = self;
    self.noFootView.addNewAddressCallBack = ^{
        weakSelf.tabBarController.selectedIndex = 0 ;
    };

}

- (void)setupCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(ScreenWidth, 90);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = RGBColor(245, 245, 245)
    ;
    
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
    
    //注册cell
    [collectionView registerNib:[UINib nibWithNibName:@"GYFootCell" bundle:nil] forCellWithReuseIdentifier:footCellIdentifier];
    
    self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}

- (void)loadMoreData {
    [self loadFootData:[NSString stringWithFormat:@"%zd",self.currentIndex]];
}

- (void)loadFootData:(NSString *)curpage {
    if (self.isHasmore) {
        [EasyLodingView showLodingText:@"正在加载..." config:^EasyLodingConfig *{
            EasyLodingConfig *config = [EasyLodingConfig shared];
            config.showOnWindow = YES;
            
            return config;
        }];
        [GYNetWorkTools requestUserFootprint:@"" page:@"20" success:^(NSDictionary *obj) {
            [self.collectionView.mj_footer endRefreshing];
            int code = [[obj objectForKey:@"code"] intValue];
            NSDictionary *datas = [obj objectForKey:@"datas"];
            if (code == 200) {
                [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
                self.currentIndex += 1;
                self.hasmore = [[datas objectForKey:@"hasmore"] boolValue];
                NSArray *goodsbrowse_list = [datas objectForKey:@"goodsbrowse_list"];
                NSArray *foots = [GYFoot loadFoot:goodsbrowse_list];
                [self.footList addObjectsFromArray:foots];
                if (self.footList.count == 0) {
                    self.noFootView.hidden = NO;
                }else {
                    self.noFootView.hidden = YES;
                    [self.collectionView reloadData];
                }
            }else if (code == 400) {
                [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
                [EasyTextView showText:@"加载数据出错,请重新加载!"];
                NSLog(@"网络加载数据=====%@",[datas objectForKey:@"error"]);
            }
            
        } error:^(NSError *error) {
            [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
            [EasyTextView showText:@"加载数据出错,请重新加载!"];
            NSLog(@"loadUserFootprintFailed====%@",error);
        }];
    }else {
        [self.collectionView.mj_footer endRefreshing];
        [EasyTextView showText:@"没有更多数据了!"];
        NSLog(@"已经没有数据加载了...");
    }
}

- (void)removeAllFoot {
    [GYNetWorkTools requestRemoveAllUserFootprint:^(NSDictionary *obj) {
        int code = [[obj objectForKey:@"code"] intValue];
        if (code == 200) {
            NSLog(@"清空用户足迹成功....");
            [EasyTextView showSuccessText:@"清空用户足迹成功!"];
            self.noFootView.hidden = NO;
            self.footList = nil;
            [self.collectionView reloadData];
            
        }else if (code == 400) {
            [EasyTextView showText:@"清空足迹失败!"];
            NSLog(@"清空用户足迹失败....");
        }
    } error:^(NSError *error) {
        [EasyTextView showText:@"清空足迹失败!"];
        NSLog(@"loadRemoveAllUserFootprintFailed===%@",error);
    }];
}

- (void)removeAllUserFootprint {
    [self removeAllFoot];
}

#pragma mark -- UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.footList.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GYFootCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:footCellIdentifier forIndexPath:indexPath];
    cell.model = self.footList[indexPath.item];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    //商品
    GYShopHomeViewController *shopHomeVC =[[GYShopHomeViewController alloc] init];
    shopHomeVC.goods_id = [self.footList[indexPath.item] goods_id];
    [self.navigationController pushViewController:shopHomeVC animated:YES];
}

#pragma mark -- 创建视图

- (GYNoAddressView *)getNoShopView:(NSString *)imageName head:(NSString *)head foot:(NSString *)foot {
    
    GYNoAddressView *shopCollectionView = [[[NSBundle mainBundle] loadNibNamed:@"GYNoAddressView" owner:nil options:nil] firstObject];
    shopCollectionView.frame = self.view.bounds;
    shopCollectionView.markImageView.image = [UIImage imageNamed:imageName];
    shopCollectionView.headLable.text = head;
    shopCollectionView.footLable.text = foot;
    shopCollectionView.hidden = YES;
    [shopCollectionView.addBtu setTitle:@"随便逛逛" forState:UIControlStateNormal];
    [self.view addSubview:shopCollectionView];
    
    return shopCollectionView;
}

#pragma mark -- 懒加载

- (NSMutableArray *)footList {
    if (!_footList) {
        _footList = [NSMutableArray array];
    }
    return _footList;
}
@end
